package cn.b.sky.mes.em.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.EquipmentCategory;
import cn.b.sky.mes.em.model.condition.EquipmentCategoryCondition;
import cn.b.sky.mes.em.service.EquipmentCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/emCategory/*")
public class EquipmentCategoryController extends SecurityController {
  @Resource
  EquipmentCategoryService equipmentCategoryService;

  @RequestMapping("emCategoryList")
  public String emCategoryList(Model model, @ModelAttribute("condition") EquipmentCategoryCondition category,
      String navTabId) {
    category.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(category.getOrderField())) {
      category.setOrderField("createDateTime");
    }
    List<EquipmentCategory> categoryList = equipmentCategoryService.selcetEquipmentCategoryByCondition(category);
    int count = equipmentCategoryService.selcetEquipmentCategoryCount(category);
    category.setData(categoryList);
    category.setTotalCount(count);
    model.addAttribute("page", category);
    model.addAttribute(NAVTABID, navTabId);
    return "em/emCategory/emCategory_list";
  }

  @RequestMapping("addCategory")
  public String addCategory(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "em/emCategory/emCategory_add";
  }

  @RequestMapping("modifyCategory")
  public String modifyCategory(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentCategory category = equipmentCategoryService.selectEquipmentCategoryByCode(getSiteCode(), id);
    model.addAttribute("category", category);
    return "em/emCategory/emCategory_add";
  }

  @RequestMapping("validateCategoryCode")
  @ResponseBody
  public boolean validateCategoryCode(String categoryCode, Model model) {
    if (!StringUtils.isEmpty(categoryCode)) {
      EquipmentCategory category = equipmentCategoryService.selectEquipmentCategoryByCode(getSiteCode(), categoryCode);
      if (category != null) {
        return false;
      }
    }
    return true;
  }

  @RequestMapping("checkCategoryCode")
  @ResponseBody
  public boolean checkCategoryCode(String categoryCode, Model model) {
    if (StringUtils.isEmpty(categoryCode)) {
      return false;
    } else {
      EquipmentCategory category = equipmentCategoryService.selectEquipmentCategoryByCode(getSiteCode(), categoryCode);
      return category != null && category.getLockFlag() != 1;
    }
  }

  @RequestMapping("saveCategory")
  @ResponseBody
  public DWZResult saveCategory(Model model, String navTabId, EquipmentCategory category, String id,
      HttpServletRequest request) {
    if (category == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    category.setSiteCode(getSiteCode());
    setCreateInfo(category);
    if (category.getLockFlag() == null) {
      category.setLockFlag(0);
    }
    if (StringUtils.isEmpty(id)) {
      equipmentCategoryService.insertEquipmentCategory(category);

      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    } else {
      equipmentCategoryService.updateEquipmentCategory(category);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);

    }

  }

  @RequestMapping("viewCategory")
  public String viewCategory(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentCategory category = equipmentCategoryService.selectEquipmentCategoryByCode(getSiteCode(), id);
    model.addAttribute("category", category);
    return "em/emCategory/emCategory_view";
  }

  @RequestMapping("deleteCategory")
  @ResponseBody
  public DWZResult deleteCategory(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = equipmentCategoryService.deleteEquipmentCategory(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping(value = "categoryLookup")
  public String categoryLookup(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentCategoryCondition condition) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    condition.setLockFlag(0);
    List<EquipmentCategory> categoryList = equipmentCategoryService.selcetEquipmentCategoryByCondition(condition);
    int count = equipmentCategoryService.selcetEquipmentCategoryCount(condition);
    condition.setData(categoryList);
    condition.setTotalCount(count);
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    model.addAttribute("page", condition);
    return "em/emCategory/emCategory_lookup";
  }

  @RequestMapping(value = "categoryDropdown")
  @ResponseBody
  public Object categoryDropdown(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentCategoryCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    return equipmentCategoryService.selcetEquipmentCategoryForSuggest(condition);
  }

}
